n = int(input())
def contain(j, k):
    count = 0
    list1 = list(str(j))
    for i in str(k):
        for l in range(len(list1)):
            if list1[l] == i: 
                list1.remove(i)
                count+=1
                break
    return True if count == len(str(j)) else False
def isSimple(n):
    count = 0
    for i in range(1, n+1):
        count += 1 if n % i == 0 else 0
    return True if count == 2 else False
def por(i, n):
    iList = list(str(i))
    nList = list(str(n))
    count = 0
    for j in range(1, len(iList)):
        if iList[j-1] == iList[j]:
            nList.remove(iList[j-1])
        if nList.index(iList[j-1]) > nList.index(iList[j]):
            count+=1
    return True if count == 0 else False
max = 0
if 1000 <= n <= 9999:
    for i in range(1, n+1):
        if (i > max and contain(i, n) and isSimple(i) and por(i, n)):
            por(i, n)
            max = i
print(max)



        
